package org.paris8.trombino.service.imp;

import java.awt.Point;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.paris8.trombino.lib.PicasawebClient;
import org.paris8.trombino.model.Album;
import org.paris8.trombino.model.Photo;
import org.paris8.trombino.service.PhotoService;
import org.paris8.trombino.util.Utils;
import org.springframework.stereotype.Service;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.TagEntry;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

@Service("PhotoService")
public class PhotoServiceImp implements PhotoService{
	protected final Logger logger = LogManager.getLogger(this.getClass());  
	private String strAcc = Utils.GetValueByNameInProperties("resources.properties", "google.userid"); 
	private String strPwd = Utils.GetValueByNameInProperties("resources.properties", "google.password"); 
	private PicasawebClient client = null;
	private List<Photo> photos = null;

	public PhotoServiceImp()
	{
		PicasawebService service = new PicasawebService("sample");
		try {
			service.setUserCredentials(strAcc, strPwd);
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		client = new PicasawebClient(service);
	}
	
	public List<Album> GetAlbumList() {
		List<Album> albums = new ArrayList<Album>();
		
		try {
			albums = GetPhotoPicasa();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return albums;
	}

	public List<Photo> GetPhotoListByAlbumId(Album album) {
		// TODO Auto-generated method stub
		return null;
	}
	
	private List<Album> GetPhotoPicasa() throws IOException, ServiceException {
		
		List<AlbumEntry> albumList = client.getAlbums(strAcc);
		
		List<Album> albums = new ArrayList<Album>();
		for(AlbumEntry ae : albumList)
		{
			Album album = new Album();
			album.setId(ae.getId());
			album.setIdNum(GetAlbumIdNum(ae.getId()));
			album.setTitle(ae.getTitle().getPlainText());
			album.setAuthor(ae.getUsername());
			
			photos = new ArrayList<Photo>();
			for(PhotoEntry pe : client.getPhotos(ae))
			{
				Photo photo = new Photo();
				photo.setId(pe.getId()); 
				photo.setIdNum(GetPhotoIdNum(pe.getId()));
				photo.setTitle(pe.getTitle().getPlainText());
				photo.setDescription(pe.getDescription().getPlainText());
				photo.setUrl(pe.getMediaContents().get(0).getUrl());
				photo.setThumbnailUrl(pe.getMediaThumbnails().get(0).getUrl());	
				
				if(pe.getGeoLocation() != null)
				{
					photo.setLocation(pe.getGeoLocation().getLatitude().toString() + ", "
									 +pe.getGeoLocation().getLongitude().toString());
				}
				photo.setTags(GetTagsByPhoto(pe));				
				
				photos.add(photo);
			}
			
			album.setPhotoList(photos);
			
			albums.add(album);
		}
		
		return albums;
	}

	public Album GetAlbumById(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	public Photo GetPhotoById(String id) {
		Photo photo = new Photo();		
		if(photos != null)
		{
			for(Photo p : photos)
			{
				if(p.getIdNum().equalsIgnoreCase(id))
				{
					photo = p;
				}
			}			
		}		
		
		return photo;
	}
	
	public String GetTagsByPhoto(PhotoEntry photo)
	{
		String ret = "";
		try {
			List<TagEntry> tags = client.getTags(photo);
			for(TagEntry tag : tags)
			{
				ret += tag.getTitle().getPlainText() + " ";				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ret;		
	}
	
	public String GetPhotoIdNum(String photoId)
	{				
		String id = photoId.substring(photoId.indexOf("photoid")+8, photoId.length());

		return id;
	}
	
	public String GetAlbumIdNum(String albumId)
	{				
		String id = albumId.substring(albumId.indexOf("albumid")+8, albumId.length());

		return id;
	}

}
